<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/30 0030
 * Time: 16:39
 */

namespace app\admin\controller;

use app\common\model\Check;
use app\common\model\GirlImg;
use app\common\model\UserId;
use think\Db;
use think\Exception;

class UserCheck extends Base
{

    function _initialize()
    {
        parent::_initialize();
        $this->assign('stamp',123);
    }

    public function index()
    {
        $where = [];
//        if(!empty($_GET['title'])){
//            $where['s.title'] = trim(input('get.title'));
//        }

        if(!empty(input('keyword'))){
            $where['u.nick_name|s.real_name'] = ['like','%'.trim(input('keyword')).'%'];
        }


        if(!isEmpty($_GET['type'])){
            $where['s.status'] = $_GET['type'];
            $this->assign('type',$_GET['type']);
        }
        $model = new UserId();
        $rows = $model->getList($where);
//        echo $rows;exit;
//        $data = Db::name('banner_cate')->where('status',1)->select();
        $this->assign([
//            'data'=>$data,
            'pageHTML'=>$rows->render(),
            'rows'=>$rows
        ]);
        return view();
    }

    public function user_check()
    {
        if(request()->isPost()){
            $data   = input('post.');
            $model  = new UserId();
            $result = $model->detail($data['id']);

            if ($data['status'] == 1){
                $str = '您的身份证认证已通过审核.';
                Db::name('users')->where('user_id',$result['user_id'])->update(['ID'=>$data['id']]);
            }else{
                $str = '您的身份证认证未通过';
            }
            $result->save(['status'=>$data['status']]);
            if($result !== false){

                \app\api\controller\Base::sendUsrMsg('system',hashid($result['user_id']),$str);

                $this->success('操作成功',url('/user_check/index'));
            }
            $this->error($result->getError());
        }
        $this->_show('user_id','ID');
        $this->assign('title','认证审核');
        return view();
    }


    /**
     * 手势认证
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function img()
    {
        $where = [];
        if (!empty($_GET['nick_name'])) $where['u.nick_name'] = ['like','%'.trim(input('get.nick_name')).'%'];
        if (!empty($_GET['register_name'])) $where['e.register_name'] = ['like','%'.trim(input('get.register_name')).'%'];
        if (!isEmpty($_GET['status'])) $where['a.status'] = input('get.status');

        if (is_numeric(input('sex'))) $where['u.sex'] = input('sex');


        if(!empty($_GET['startDate'])&&empty($_GET['endDate'])){
            $where['a.create_time'] = ['>=',strtotime($_GET['startDate'])];
        }elseif(!empty($_GET['endDate'])&&empty($_GET['startDate'])){
            $where['a.create_time'] = ['<=',strtotime($_GET['endDate'])];
        }elseif(!empty($_GET['startDate'])&&!empty($_GET['endDate'])){
            $where['a.create_time'] = ['between',array(strtotime($_GET['startDate']),strtotime($_GET['endDate']))];
        }

        $model  = new GirlImg();
        $rows   = $model->getList($where);
        $this->assign([
            'rows' => $rows,
            'title' => '手势认证列表',
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }


    /**
     * 审核批量操作
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function batch()
    {
        $ids = input('id/a');
        $type = input('type');
        switch ($type){
            case 'delete':
                $result = GirlImg::destroy($ids);
                break;
            default:

                Db::startTrans();
                try{

                    foreach ($ids as $k => $id){
                        $type1 = ['type'=>'text','data'=>null];
                        if ($type == 'success'){
                            $status = 1;
                            $img = 1;
                            $str = '已通过审核';
                        }elseif ($type == 'send'){
                            $img = 3;
                            $status = 2;
                            $str = '已通过审核';
                        }
                        else {
                            $img = 0;
                            $str = '未通过审核,请重新上传';
                            $status = 0;
                            $type1 = ['type'=>'real_check','data'=>null];
                        }

                        $info = Db::name('girl_img')->where('id',$id)->findOrFail();

                        if ($info['status'] != 2){
                            continue;
                        }

                        $msg = sprintf('您的真人认证%s',$str);

                        \app\api\controller\Base::sendUsrMsg('system',hashid($info['user_id']),$msg,$type1);

                        Db::name('girl_img')->where('id',$id)->update(['status'=>$img]);
                        Db::name('users')->where('user_id',$info['user_id'])->update(['is_real'=>$status]);
                        Db::commit();

                    }
                }catch (Exception $e){
                    Db::rollback();
                    $this->error($e->getMessage());
                }

                $this->success('操作成功');

                break;
        }
        if ($result !== false){
            $this->success('操作成功');
        }

        $this->error('操作失败');

    }



    public function change()
    {
        $data = input();
        $result = Db::name('girl_img')->update(['status'=>$data['type'],'id'=>$data['id']]);
        if($result !== false){
            $info = Db::name('girl_img')->where('id',$data['id'])->find();

            $type = ['type'=>'text','data'=>null];
            if ($data['type'] == 1){
                $img = 1;
                $str = '已通过审核';
            }elseif ($data['type'] == 2){
                //发放真人图标
                $str = '已通过审核';
                $img = 3;
            }
            else{
                $img = 0;
                $str = '因['.$data['msg'].']被驳回;';
                $type = ['type'=>'real_check','data'=>null];
            }
            Db::name('girl_img')->where('id',$data['id'])->update(['status'=>$img]);
            Db::name('users')->where('user_id',$info['user_id'])->update(['is_real'=>$data['type']]);

            $msg = sprintf('您真人认证%s',$str);

            \app\api\controller\Base::sendUsrMsg('system',hashid($info['user_id']),$msg,$type);
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    public function delete()
    {
        $id = input('id');
        $file = Db::name('girl_img')->where(['id'=>$id])->find();

        if (!$file){
            $this->error('参数不存在');
        }

        Db::startTrans();
        try{

            Db::name('girl_img')->where('id', $id)->delete();

            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error('操作失败');
        }
        $this->success('删除成功');
    }



}
